package game.breeze.gameserver.message.processor;

import game.breeze.Constants.Constants;
import game.breeze.astar.AStarNode;
import game.breeze.gameserver.World;
import game.breeze.gameserver.controller.GameMapController;
import game.breeze.gameserver.controller.PlayerController;
import game.breeze.message.AppMessage;
import game.breeze.message.SceneMessageCode;
import game.breeze.socket.util.ByteBuffer;

import java.util.List;

public class SceneMessageProcessor implements IMessageProcessor {
	private static final SceneMessageProcessor instance = new SceneMessageProcessor();

	public static final SceneMessageProcessor getInstance() {
		return instance;
	}

	private SceneMessageProcessor() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * game.breeze.gameserver.message.manager.IMessageManager#proccessMessage
	 * (game.breeze.gameserver.controller.HeroController,
	 * game.breeze.message.AppMessage)
	 */
	@Override
	public void proccessMessage(PlayerController controller, AppMessage message) {
		ByteBuffer buffer = message.getBuffer();
		int type = buffer.readInt();
		switch (type) {
		case SceneMessageCode.MSG_SCENE_PLAYER_FIND_PATH:
			proccessFindPath(controller, message);
			break;
		case SceneMessageCode.MSG_SCENE_PLAYER_MOVE:
			proccessPlayerMove(controller, message);
			break;
		default:
			break;
		}
	}

	/**
	 * 玩家移动
	 * 
	 * @param controller
	 *            玩家
	 * @param message
	 *            消息
	 */
	private void proccessPlayerMove(PlayerController controller, AppMessage message) {
		ByteBuffer buffer = message.getBuffer();
		int map = buffer.readInt();
		int x = buffer.readInt();
		int y = buffer.readInt();
		controller.getPlayer().setX(x);
		controller.getPlayer().setY(y);
		GameMapController mapController = World.getInstance().getMapController(map);
		if (mapController == null) {
			System.out.println("不存在的地图:" + map);
			return;
		}
		// 同步玩家数据
		mapController.synPlayerMove(controller);
	}

	/**
	 * 寻路
	 * 
	 * @param controller
	 *            玩家
	 * @param message
	 *            消息
	 */
	private void proccessFindPath(PlayerController controller, AppMessage message) {
		ByteBuffer buffer = message.getBuffer();
		int map = buffer.readInt();
		int x = buffer.readInt();
		int y = buffer.readInt();
		int dx = buffer.readInt();
		int dy = buffer.readInt();

		World world = (World) message.getTransient(Constants.MESSAGE_TRANSIENT_WORLD);
		GameMapController mapController = world.getMapController(map);
		List<AStarNode> path = mapController.findPath(x, y, dx, dy);
		// List<AStarNode> path = new ArrayList<AStarNode>();
		AppMessage returnMsg = AppMessage.createMessage(AppMessage.MSG_SCENE);
		returnMsg.getBuffer().writeInt(SceneMessageCode.MSG_SCENE_PLAYER_FIND_PATH);
		returnMsg.getBuffer().writeInt(path.size());
		for (int i = 0; i < path.size(); i++) {
			AStarNode node = path.get(i);
			returnMsg.getBuffer().writeInt(node.getRow());
			returnMsg.getBuffer().writeInt(node.getCol());
		}
		path.clear();
		controller.sendMessage(returnMsg);
	}
}
